package com.fang.springcrud.controller;

import com.fang.springcrud.entity.UserEntity;
import com.fang.springcrud.resp.BaseResponse;
import com.fang.springcrud.resp.ResponseBean;
import com.fang.springcrud.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

@RestController
@RequestMapping("/generator")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user/list")
    public ResponseBean list(int page, int limit, String key) {
        ResponseBean bean = ResponseBean.SUCCESS;
        ResponseBean.Page page1 = userService.userList(page, limit, key);
        bean.setPage(page1);
        return bean;
    }

    /**
     * 获得用户详情信息
     *
     * @param id
     */
    @GetMapping("/user/info/{id}")
    public BaseResponse list(@PathVariable String id) {
        BaseResponse baseResponse = new BaseResponse(0);
        UserEntity userEntity = userService.selectForId(id);
        return baseResponse.set("user", userEntity);
    }

    @PostMapping("/user/update")
    public BaseResponse update(@RequestBody UserEntity entity) {
        userService.updateEntity(entity);
        return new BaseResponse(0);
    }

    @PostMapping("/user/delete")
    public BaseResponse delete(@RequestBody int[] ids) {
        userService.deleteForIds(ids);
        return new BaseResponse(0);
    }

    @PostMapping("/user/save")
    public BaseResponse save(@RequestBody UserEntity entity) {
        userService.save(entity);
        return new BaseResponse(0);
    }

}
